		TITLE EXTDEF - Copyright (c) 1988-94 by SLR Systems

		INCLUDE MACROS
		INCLUDE	SYMBOLS
		INCLUDE	CDDATA

		PUBLIC	EXTDEF,LEXTDF,PROCESS_EXTERNALS,REFERENCE_COMDAT


		.DATA

		EXTERNDEF	TEMP_RECORD:BYTE,SYMBOL_TEXT:BYTE

		EXTERNDEF	SYM_HASH_MOD:DWORD,END_OF_RECORD:DWORD,CURNMOD_GINDEX:DWORD,BUFFER_OFFSET:DWORD,SOFT_SEGM:DWORD
		EXTERNDEF	LAST_EXTDEF_GINDEX:DWORD,CURN_SEGMOD_GINDEX:DWORD,SYMBOL_LENGTH:DWORD

		EXTERNDEF	SYMBOL_LARRAY:LARRAY_STRUCT,SYMBOL_GARRAY:STD_PTR_S,SEGMOD_GARRAY:STD_PTR_S,SYMBOL_TPTR:TPTR_STRUCT

		EXTERNDEF	OPTI_MOVE:DWORD
                EXTERNDEF	LAST_MODULE_GINDEX_EXT:DWORD

		.CODE	PASS1_TEXT

		EXTERNDEF	FAR_INSTALL:PROC,OBJ_PHASE:PROC,LIB_SYM_REQUEST:PROC,ADD_TO_EXTERNAL_LIST:PROC
		EXTERNDEF	ADD_TO_WEAK_LIST:PROC,_reference_libsym:proc,UNDO_ALIAS:PROC,DO_DEFINE_MODULE:PROC
		EXTERNDEF	REMOVE_FROM_WEAK_LIST:PROC,STORE_XREF_ENTRY:PROC,GET_SM_MODULE:PROC,MOVE_ASCIZ_ESI_EDI:PROC
		EXTERNDEF	REMOVE_FROM_WEAK_DEFINED_LIST:PROC,ADD_TO_COMMUNAL_LIST:PROC,REMOVE_FROM_LAZY_LIST:PROC
		EXTERNDEF	REMOVE_FROM_LAZY_DEFINED_LIST:PROC,ADD_TO_LAZY_LIST:PROC,ADD_TO_ALIASED_LIST:PROC
		EXTERNDEF	ADD_TO__IMP__LIST:PROC,SAY_OMF_VERBOSE:PROC,SAY_VERBOSE:PROC,HEXWOUT:PROC


LEXTDF		PROC
		;
		;
		;
		MOV	EAX,CURNMOD_GINDEX
		TEST	EAX,EAX
		MOV	SYM_HASH_MOD,EAX
		JZ	L5$
		CALL	TEST_EXT
		XOR	EAX,EAX
		MOV	SYM_HASH_MOD,EAX
		RET

L5$:
		CALL	DO_DEFINE_MODULE
		JMP	LEXTDF

LEXTDF		ENDP


EXTDEF		PROC		;USES
		;
		;THIS IS RECORD CONTAINING NAME(S) USED BY OTHER RECORDS
		;AS EXTERNAL REFERENCES
		;
		;INSERT SYMBOL IN SYMBOL TABLE.
		;INSERT POINTER IN EXTDEF TABLE.
		;REPEAT WHILE MORE NAMES
		;
		;DS:SI IS RECORD POINTER
		;
		;DON'T FORGET TO UPPER CASE IT IF NOT RETAINING SIGNIFICANCE.
		;
		MOV	EAX,CURNMOD_GINDEX
		TEST	EAX,EAX
		JNZ	TEST_EXT
		CALL	DO_DEFINE_MODULE
		JMP	TEST_EXT

EXT_LOOP:
		MOV	EDI,OFF SYMBOL_TPTR
		ASSUME	EDI:PTR TPTR_STRUCT

		GET_NAME_HASH			;PUT IN SYMBOL_TEXT
		SKIP_INDEX
		MOV	BUFFER_OFFSET,ESI

		CALL	FAR_INSTALL		;EAX IS INDEX, ECX IS SYMBOL PHYSICAL
if	fg_td
		MOV	LAST_EXTDEF_GINDEX,EAX	;FOR BORLAND INFO
endif

		INSTALL_GINDEX_LINDEX	SYMBOL_LARRAY		;STORE PLEASE
if	fg_xref
		GETT	BL,XREF_OUT
		MOV	BH,[ECX].SYMBOL_STRUCT._S_REF_FLAGS		;LOCAL SYMBOL?
		OR	BL,BL
		JZ	L3$
		AND	BH,MASK S_SPACES
		JNZ	L3$
		CALL	STORE_XREF_ENTRY
L3$:
endif

TEST_EXT	LABEL	PROC

		CMP	END_OF_RECORD,ESI
		JA	EXT_LOOP
		JNZ	ERROR
		RET

ERROR:
		CALL	OBJ_PHASE
		RET

EXTDEF		ENDP


PROCESS_EXTERNALS	PROC
		;
		;END OF MODULE, DECIDE WHAT TO DO WITH MY EXTERNALS...
		;
		MOV	EDX,SYMBOL_LARRAY._LARRAY_LIMIT
		XOR	EAX,EAX

		TEST	EDX,EDX
		JZ	L9$

		PUSHM	EDI,ESI,EBX
		INC	EAX			;FIRST INDEX NUMBER
L1$:
		PUSHM	EDX,EAX

;		PUSH	EAX
;		MOV	EAX,OFF PROCESSING_EXTRN
;		CALL	SAY_OMF_VERBOSE
;		POP	EAX

		CONVERT_LINDEX_EAX_EAX	SYMBOL_LARRAY,ESI	;RETURNS POINTER

		CONVERT	ECX,EAX,SYMBOL_GARRAY
		ASSUME	ECX:PTR SYMBOL_STRUCT
		CALL	PROCESS_EXTRN_ECXEAX

		POP	EAX
		AND	BL,NOT (MASK S_HARD_REF+MASK S_SOFT_REF+MASK S_WEAK_AGAIN)

		POP	EDX
		MOV	[ECX]._S_REF_FLAGS,BL

		INC	EAX
		DEC	EDX

		JNZ	L1$

		POPM	EBX,ESI,EDI
L9$:
		RET

PROCESS_EXTERNALS	ENDP

if	debug

SAY_EXTRN_TYPE	PROC

		BITT	VERBOSE_OMF
		JZ	L9$

		PUSHM	EDI,EDX,ECX,EAX

		MOV	EAX,EDX

		MOV	EDI,OFF OMF_TYPE_HEX
		CALL	HEXWOUT

		MOV	EAX,OFF SAY_OMF_TYP_MSG
		CALL	SAY_VERBOSE

		POPM	EAX,ECX,EDX,EDI

L9$:
		RET

SAY_EXTRN_TYPE	ENDP
endif


PROCESS_EXTRN_ECXEAX	PROC
		;
		;GO THRU JUMP TABLE:
		;
		;	1. IGNORE ANY THAT ARE 'DEFINED'
		;	2. IGNORE 'PROMISED'
		;	3. IGNORE DEFAULTS
		;	4. REFERENCE UNDEFINED, LIBRARY, WEAK_EXTRN
		;	5. CHANGE POS_WEAK TO WEAK_EXTRN
		;
		XOR	EDX,EDX

		MOV	DL,[ECX]._S_NSYM_TYPE

		MOV	BL,[ECX]._S_REF_FLAGS		;BL = REF_FLAGS

;		CALL	SAY_EXTRN_TYPE

		JMP	PE_TABLE[EDX*2]

L6$:
L7$:
		TEST	BL,MASK S_HARD_REF+MASK S_SOFT_REF
		JZ	L72$			;NOT REFERENCED AT ALL, ITS A HARD REFERENCE
		TEST	BL,MASK S_HARD_REF
		JZ	L8$			;ONLY SOFT, IGNORE
L72$:
		OR	BL,MASK S_REFERENCED
L8$:
		RET

PE_NSYM_WEAK_UNREF:
		;
		;
		;
		TEST	BL,MASK S_WEAK_AGAIN
		JZ	PE_NSYM_UNDEFINED

		TEST	BL,MASK S_HARD_REF+MASK S_SOFT_REF
		JZ	PNWU_1			;NOT REFERENCED AT ALL, ITS A HARD REF

		TEST	BL,MASK S_HARD_REF
		JZ	L8$			;ONLY SOFT, IGNORE, LEAVE IT WEAK-UNREF FOR NOW
PNWU_1:
		OR	BL,MASK S_REFERENCED
		JMP	ADD_TO_WEAK_LIST


PE_NSYM_WEAK_DEFINED:
		;
		;IN WEAK-DEFINED LIST, DID WE MARK IT WEAK TOO?
		;
		TEST	BL,MASK S_WEAK_AGAIN
		JNZ	L7$			;YES, STILL WEAK

		TEST	BL,MASK S_HARD_REF+MASK S_SOFT_REF
		JZ	PNWDE			;NOT REFERENCED AT ALL, ITS A HARD REFERENCE

		TEST	BL,MASK S_HARD_REF
		JZ	L8$			;ONLY SOFT, IGNORE, LEAVE IT WEAK FOR NOW
PNWDE:
		CALL	REMOVE_FROM_WEAK_DEFINED_LIST
		JMP	MR_3

PE_NSYM_WEAK_EXTRN:
		;
		;IN WEAK EXTRN LIST, DID WE MARK IT WEAK TOO?
		;
		TEST	BL,MASK S_WEAK_AGAIN
		JNZ	L7$			;YES, STILL WEAK
MAKE_REAL:
		;
		;NOT WEAK, DID I GIVE IT A HARD REFERENCE THIS MODULE?
		;
		TEST	BL,MASK S_HARD_REF+MASK S_SOFT_REF
		JZ	MR_2			;NOT REFERENCED AT ALL, ITS A HARD REFERENCE
		TEST	BL,MASK S_HARD_REF
		JZ	L8$			;ONLY SOFT, IGNORE, LEAVE IT WEAK FOR NOW
MR_2:
		CALL	REMOVE_FROM_WEAK_LIST
MR_3:
		CMP	[ECX]._S_LIB_GINDEX,0
		JNZ	PE_NSYM_LIBRARY
PE_NSYM_UNDEFINED:
		;
		;ADD TO REFERENCED EXTERNALS LIST
		;
		TEST	BL,MASK S_HARD_REF+MASK S_SOFT_REF
		JZ	PNU_2			;NOT REFERENCED AT ALL, ITS A HARD REFERENCE

		TEST	BL,MASK S_HARD_REF
		JZ	L8$			;ONLY SOFT, IGNORE
PNU_2:
		MOV	EDX,LAST_MODULE_GINDEX_EXT		;BECAUSE PROCESS EXTERNALS MESSES UP CURNMOD_GINDEX
		OR	BL,MASK S_REFERENCED
		MOV	CURNMOD_GINDEX,EDX
		JMP	ADD_TO_EXTERNAL_LIST	;ADD TO END OF EXTERNAL LIST


PE_NSYM_LIBRARY:
		;
		;LINK TO CORRECT LIBRARY PLEASE IF HARD REFERENCE
		;
		TEST	BL,MASK S_HARD_REF+MASK S_SOFT_REF
		JZ	PNL_2			;NOT REFERENCED AT ALL, ITS A HARD REFERENCE
		TEST	BL,MASK S_HARD_REF
		JZ	L8$			;ONLY SOFT, IGNORE
PNL_2:
		OR	BL,MASK S_REFERENCED

		push	ECX
		push	EAX
		push	ECX
		push	EAX
		call	_reference_libsym		;REFERENCE A LIBRARY SYMBOL
		add	ESP,8
		pop	EAX
		pop	ECX
		ret


PE_NSYM_POS_WEAK:
		;
		;WE MARKED THIS UNDEFINED SYMBOL AS WEAK, ADD IT TO WEAK LIST IF REFERENCED, ELSE MARK IT WEAK_UNREF
		;
		TEST	BL,MASK S_HARD_REF+MASK S_SOFT_REF
		JZ	PNPW_1			;NOT REFERENCED AT ALL, ITS A HARD REFERENCE
		TEST	BL,MASK S_HARD_REF
		JZ	PNPW_2			;ONLY SOFT, IGNORE
PNPW_1:
		OR	BL,MASK S_REFERENCED
		JMP	ADD_TO_WEAK_LIST

PNPW_2:
		MOV	[ECX]._S_NSYM_TYPE,NSYM_WEAK_UNREF
		JMP	L8$

PE_NSYM_COMDAT:
		;
		;CHECK THIS COMDAT
		;
		TEST	BL,MASK S_REFERENCED		;ALREADY HARD REFERENCED?
		JNZ	PNC_9				;YES, IGNORE
		TEST	BL,MASK S_SOFT_REF		;WAS IT SOFT REFERENCED?
		JZ	PNC_5				;NO, THEN IT IS HARD
		TEST	BL,MASK S_HARD_REF		;SOFT, WAS IT ALSO HARD?
		JZ	PNC_9				;NO, SOFT ONLY, JUST SET FLAGS
PNC_5:
		;
		;NEW HARD REFERENCE, IF PACKING, REFERENCE ALL MY SOFT EXTERNS...
		;
		BITT	PACKFUNCTIONS_FLAG
		JZ	PNC_9
		CALL	REFERENCE_COMDAT
		OR	BL,MASK S_REFERENCED
PNC_9:
		RET


PE_NSYM_ALIAS_UNREF:
		;
		;UNREFERENCED ALIAS
		;
		TEST	BL,MASK S_SOFT_REF
		JZ	PNAU_1
		TEST	BL,MASK S_HARD_REF
		JZ	PNC_9
PNAU_1:
		OR	BL,MASK S_REFERENCED
		JMP	ADD_TO_ALIASED_LIST


PE_NSYM_POS_LAZY:
		;
		;WE MARKED THIS UNDEFINED SYMBOL AS LAZY, ADD IT TO LAZY LIST IF REFERENCED, ELSE MARK IT LAZY_UNREF
		;
		TEST	BL,MASK S_HARD_REF+MASK S_SOFT_REF
		JZ	PNPL_1			;NOT REFERENCED AT ALL, ITS A HARD REFERENCE
		TEST	BL,MASK S_HARD_REF
		JZ	PNPL_2			;ONLY SOFT, IGNORE
PNPL_1:
		OR	BL,MASK S_REFERENCED
		JMP	ADD_TO_LAZY_LIST

PNPL_2:
		MOV	[ECX]._S_NSYM_TYPE,NSYM_LAZY_UNREF
		JMP	L8$

PE_NSYM_LAZY_UNREF:
		;
		;
		;
		TEST	BL,MASK S_WEAK_AGAIN
		JZ	PE_NSYM_UNDEFINED
		TEST	BL,MASK S_HARD_REF+MASK S_SOFT_REF
		JZ	PNLU_1			;NOT REFERENCED AT ALL, ITS A HARD REF
		TEST	BL,MASK S_HARD_REF
		JZ	L8$			;ONLY SOFT, IGNORE, LEAVE IT WEAK-UNREF FOR NOW
PNLU_1:
		OR	BL,MASK S_REFERENCED
		JMP	ADD_TO_LAZY_LIST


PE_NSYM__IMP__UNREF:
		;
		;
		;
		TEST	BL,MASK S_HARD_REF+MASK S_SOFT_REF
		JZ	PNIU_1			;NOT REFERENCED AT ALL, ITS A HARD REF
		TEST	BL,MASK S_HARD_REF
		JZ	L8$			;ONLY SOFT, IGNORE, LEAVE IT IMP-UNREF FOR NOW
PNIU_1:
		OR	BL,MASK S_REFERENCED
		JMP	REFERENCE_IMPSYM


PE_NSYM_LAZY_DEFINED:
		;
		;IN LAZY-DEFINED LIST, DID WE MARK IT WEAK TOO?
		;
		TEST	BL,MASK S_WEAK_AGAIN
		JNZ	L7$			;YES, STILL LAZY
		TEST	BL,MASK S_HARD_REF+MASK S_SOFT_REF
		JZ	PNLDE			;NOT REFERENCED AT ALL, ITS A HARD REFERENCE
		TEST	BL,MASK S_HARD_REF
		JZ	L8$			;ONLY SOFT, IGNORE, LEAVE IT LAZY FOR NOW
PNLDE:
		CALL	REMOVE_FROM_LAZY_DEFINED_LIST
		JMP	MR_3

PE_NSYM_LAZY:
		;
		;IN LAZY LIST, DID WE MARK IT WEAK TOO?
		;
		TEST	BL,MASK S_WEAK_AGAIN
		JNZ	L7$			;YES, STILL LAZY
		;
		;NOT LAZY, DID I GIVE IT A HARD REFERENCE THIS MODULE?
		;
		TEST	BL,MASK S_HARD_REF+MASK S_SOFT_REF
		JZ	ML_2			;NOT REFERENCED AT ALL, ITS A HARD REFERENCE
		TEST	BL,MASK S_HARD_REF
		JZ	L8$			;ONLY SOFT, IGNORE, LEAVE IT LAZY FOR NOW
ML_2:
		CALL	REMOVE_FROM_LAZY_LIST
		JMP	MR_3

PE_NCOMM_UNREF:
PE_FCOMM_UNREF:
PE_HCOMM_UNREF:
		;
		;UNREFERENCED COMDEFS, DID WE REFERENCE THEM?
		;
		TEST	BL,MASK S_HARD_REF+MASK S_SOFT_REF
		JZ	PFU_2			;NO REFERENCE = HARD
		TEST	BL,MASK S_HARD_REF
		JZ	L8$
PFU_2:
		SUB	[ECX]._S_NSYM_TYPE,NSYM_NCOMM_UNREF-NSYM_COMM_NEAR
		OR	BL,MASK S_REFERENCED

		JMP	ADD_TO_COMMUNAL_LIST


		.CONST

		ALIGN	4

PE_TABLE	LABEL	DWORD

		DD	PE_NSYM_UNDEFINED	;GO ADD TO EXTRN LIST - IF NOT SOFT REFERENCED...
		DD	L7$ ;PE_NSYM_ASEG	;DEFINED, IGNORE
		DD	L7$ ;PE_NSYM_RELOCATABLE	;DEFINED, IGNORE
		DD	L7$ ;PE_NSYM_COMMUNAL	;DEFINED, IGNORE
		DD	L7$			;FAR COMMUNAL
		DD	L7$			;HUGE COMMUNAL
		DD	L7$ ;PE_NSYM_CONST	;DEFINED, IGNORE
		DD	PE_NSYM_LIBRARY		;IN A LIBRARY, 'REFERENCE' IT IT HARD
		DD	L7$ ;PE_NSYM_IMPORT	;DEFINED, IGNORE
		DD	L7$ ;PE_NSYM_PROMISED	;PROMISED, IGNORE
		DD	L8$ ;PE_NSYM_EXTERNAL	;ALREADY IN EXTRN LIST, IGNORE
		DD	PE_NSYM_WEAK_EXTRN	;IN 'WEAK' LIST, SEE IF 'WEAK' AGAIN
		DD	PE_NSYM_POS_WEAK	;ADD TO WEAK LIST
		DD	L8$ ;PE_NSYM_LIBRARY_LIST;ALREADY IN REFERENCED LIBRARY LIST
		DD	PE_NSYM__IMP__UNREF
		DD	L7$			;REFERENCED ALIAS, IGNORE
		DD	PE_NSYM_COMDAT		;COMDAT
		DD	PE_NSYM_WEAK_DEFINED	;IN WEAK-DEFINED LIST, SEE IF WEAK AGAIN
		DD	PE_NSYM_WEAK_UNREF	;WEAK-UNREF, SEE IF WEAK AGAIN, REFERENCED, ETC
		DD	PE_NSYM_ALIAS_UNREF	;ALIAS-UNREF, MAYBE REFERENCE IT
		DD	PE_NSYM_POS_LAZY	;LAZY IN-THE-MAKING
		DD	PE_NSYM_LAZY		;LAZY LIST, SEE IF STILL LAZY...
		DD	PE_NSYM_LAZY_UNREF	;LAZY_UNREF, SEE IF REFERENCED
		DD	L7$			;REFERENCED ALIAS-DEFINED, IGNORE
		DD	PE_NSYM_LAZY_DEFINED	;LAZY-DEFINED, SEE IF STILL LAZY
		DD	PE_NCOMM_UNREF		;NEAR_COMMUNAL-UNREF, SEE IF REFERENCED
		DD	PE_FCOMM_UNREF		;FAR COMM-UNREF, SEE IF REFERENCED
		DD	PE_HCOMM_UNREF		;HUGE COMM-UNREF, SEE IF REFERENCED
		DD	L8$			;__imp__, ALREADY REFERENCED, IGNORE
		DD	L7$			;UNDECORATED, IGNORE

.ERRNZ	($-PE_TABLE)-NSYM_SIZE*2

		.CODE	PASS1_TEXT

PROCESS_EXTRN_ECXEAX	ENDP


		public	_reference_comdat
_reference_comdat	proc
		mov	ECX,8[ESP]
		mov	EAX,4[ESP]
_reference_comdat	endp

REFERENCE_COMDAT	PROC
		;
		;EAX IS GINDEX, ECX IS PHYSICAL
		;RETURN SAME...
		;
		ASSUME	ECX:PTR SYMBOL_STRUCT

		TEST	[ECX]._S_REF_FLAGS,MASK S_REFERENCED		;SKIP IF ALREADY REFERENCED
		JNZ	L9$

		MOV	EDX,[ECX]._S_CD_SEGMOD_GINDEX
		CONVERT	EDX,EDX,SEGMOD_GARRAY
		ASSUME	EDX:PTR CDSEGMOD_STRUCT

		CMP	[EDX]._CDSM_SOFT_BLOCK,0
		JZ	L8$

		XOR	EDX,EDX
		PUSHM	EAX,ECX
		MOV	CURN_SEGMOD_GINDEX,EDX

		PUSHM	EDI,ESI,EBX

		CALL	REF_COMDAT

		POPM	EBX,ESI,EDI

		POPM	ECX,EAX
L9$:
		RET

L8$:
		OR	[ECX]._S_REF_FLAGS,MASK S_REFERENCED

		RET

REFERENCE_COMDAT	ENDP


REF_COMDAT	PROC	NEAR
		;
		;ECX (EAX) IS SYMBOL, REFERENCE ALL OF MY EXTERNALS
		;CURN_SEGMOD IS PREVIOUS SEGMOD IN NESTING
		;EDX	IS COUNT LEFT IN PREVIOUS SOFT_BLOCK
		;
		MOV	AL,[ECX]._S_REF_FLAGS
		MOV	EBX,EDX

		TEST	AL,MASK S_REFERENCED
		JNZ	CHT_IGNORE

		MOV	EDX,[ECX]._S_CD_SEGMOD_GINDEX
		OR	AL,MASK S_REFERENCED

		CONVERT	ESI,EDX,SEGMOD_GARRAY
		ASSUME	ESI:PTR CDSEGMOD_STRUCT
		MOV	[ECX]._S_REF_FLAGS,AL

		MOV	EAX,CURN_SEGMOD_GINDEX

		CMP	[ESI]._CDSM_SOFT_BLOCK,0
		JZ	CHT_IGNORE
		;
		;THERE ARE SOME SOFT EXTERN REFERENCES
		;
		MOV	[ESI]._CDSM_PREV_COUNT,EBX
		MOV	CURN_SEGMOD_GINDEX,EDX
		MOV	[ESI]._CDSM_PREV_SEGMOD_GINDEX,EAX
		;
		;DEFINE CURNMOD
		;
		MOV	EAX,ESI
		CALL	GET_SM_MODULE

		MOV	CURNMOD_GINDEX,EAX
L1$:
		MOV	EDI,[ESI]._CDSM_SOFT_BLOCK
		ASSUME	EDI:NOTHING

		ADD	EDI,8				;SKIP LINK TO NEXT, COUNT

		MOV	EBX,[EDI-4]			;CHECK EBX SYMBOLS
L2$:
		MOV	ECX,[EDI+4]			;GET NEXT SYMBOL

		MOV	EAX,ECX

		CONVERT	ECX,ECX,SYMBOL_GARRAY
		ASSUME	ECX:PTR SYMBOL_STRUCT

		MOV	ESI,DPTR [ECX]._S_NSYM_TYPE

		AND	ESI,NSYM_ANDER
		ADD	EDI,8

		MOV	EDX,EBX				;IN CASE OF COMDAT REFERENCE

		JMP	COMDAT_HARD_TABLE[ESI*2]

CHT_REFERENCE:
		OR	[ECX]._S_REF_FLAGS,MASK S_REFERENCED
CHT_IGNORE:
		DEC	EBX				;MORE IN THIS SOFT_BLOCK?
		JNZ	L2$				;YES, JUMP

		MOV	ESI,CURN_SEGMOD_GINDEX

		TEST	ESI,ESI
		JZ	L9$

		CONVERT	ESI,ESI,SEGMOD_GARRAY
		ASSUME	ESI:PTR CDSEGMOD_STRUCT

		MOV	EDI,[ESI]._CDSM_SOFT_BLOCK

		MOV	EAX,[EDI]

		MOV	[ESI]._CDSM_SOFT_BLOCK,EAX

		TEST	EAX,EAX
		JNZ	L1$
		;
		;NO MORE SYMBOLS REFERENCED FROM THIS COMDAT
		;
		MOV	EAX,[ESI]._CDSM_PREV_SEGMOD_GINDEX	;WAS THERE A PREVIOUS?
		MOV	EBX,[ESI]._CDSM_PREV_COUNT

		MOV	EDX,EAX
		MOV	CURN_SEGMOD_GINDEX,EAX

		TEST	EDX,EDX
		JZ	L9$				;NO, DONE

		CONVERT	EAX,EAX,SEGMOD_GARRAY		;PREVIOUS IS NOW CURRENT
		MOV	ESI,EAX
		;
		;	DEFINE CURNMOD
		;
		CALL	GET_SM_MODULE

		MOV	CURNMOD_GINDEX,EAX
		MOV	EDI,[ESI]._CDSM_SOFT_BLOCK

		MOV	EAX,[EDI+4]			;# IN RECORD

		SUB	EAX,EBX				;# USED UP

		LEA	EDI,[EDI+EAX*8+16]
		JMP	CHT_IGNORE

L9$:
		RET

CHT_UNDEF:
		;
		;ECX IS SYMBOL, EAX IS GINDEX
		;
		CALL	ADD_TO_EXTERNAL_LIST
		JMP	CHT_REFERENCE

CHT_REF_IMP:
		;
		;
		;
		CALL	REFERENCE_IMPSYM
		JMP	CHT_REFERENCE

CHT_REF_LIB:
		;
		;ECX IS SYMBOL, EAX IS GINDEX
		;
		push	ECX
		push	EAX
		push	ECX
		push	EAX
		call	_reference_libsym
		add	ESP,8
		pop	EAX
		pop	ECX
CRL_1:
		JMP	CHT_REFERENCE

CRL_2:
		OR	[ECX]._S_REF_FLAGS,MASK S_HARD_REF
		JMP	CHT_IGNORE

CHT_WEAK_DEFINED:
		;
		;ECX IS SYMBOL, EAX IS GINDEX, [DI-4] PTS TO IMPORTANT DATA...
		;
		CALL	CHECK_WEAKNESS		;Z MEANS OK TO LEAVE IT WEAK
		JZ	CRL_1
		CALL	REMOVE_FROM_WEAK_DEFINED_LIST
		JMP	CW_2

CHT_WEAK:
		;
		;DX:BX IS SYMBOL, DS IS PHYSICAL, ES:[DI-2] PTS TO IMPORTANT DATA...
		;
		CALL	CHECK_WEAKNESS		;Z MEANS OK TO LEAVE IT WEAK
		JZ	CRL_1
		CALL	REMOVE_FROM_WEAK_LIST
CW_2:
CW_9:
		CMP	[ECX]._S_LIB_GINDEX,0	;A LIBRARY ASSIGNED?
		JNZ	CHT_REF_LIB
		JMP	CHT_UNDEF

CHT_POSWEAK:
		CALL	CHECK_WEAKNESS		;Z MEANS OK TO LEAVE IT WEAK
		JZ	CRL_2
		JMP	CW_9

CHT_WEAK_UNREF:
		CALL	CHECK_WEAKNESS
		JNZ	CW_9
		CALL	ADD_TO_WEAK_LIST
		JMP	CHT_REFERENCE

CHT_ALIASED_UNREF:
		CALL	ADD_TO_ALIASED_LIST
		JMP	CHT_REFERENCE

CHT_NCOMM_UNREF:
CHT_FCOMM_UNREF:
CHT_HCOMM_UNREF:
		SUB	[ECX]._S_NSYM_TYPE,NSYM_NCOMM_UNREF-NSYM_COMM_NEAR
		CALL	ADD_TO_COMMUNAL_LIST
CFU_1:
		JMP	CHT_REFERENCE

CHT_LAZY_DEFINED:
		;
		;DX:BX IS SYMBOL, DS IS PHYSICAL, ES:[DI-2] PTS TO IMPORTANT DATA...
		;
		CALL	CHECK_LAZINESS		;Z MEANS OK TO LEAVE IT LAZY
		JZ	CFU_1
		CALL	REMOVE_FROM_LAZY_DEFINED_LIST
		JMP	CW_2

CHT_LAZY:
		;
		;DX:BX IS SYMBOL, DS IS PHYSICAL, ES:[DI-2] PTS TO IMPORTANT DATA...
		;
		CALL	CHECK_LAZINESS		;Z MEANS OK TO LEAVE IT LAZY
		JZ	CFU_1
		CALL	REMOVE_FROM_LAZY_LIST
		JMP	CW_2

CHT_POSLAZY:
		CALL	CHECK_LAZINESS		;Z MEANS OK TO LEAVE IT LAZY
		JZ	CRL_2
		JMP	CW_9

CHT_LAZY_UNREF:
		CALL	CHECK_LAZINESS
		JNZ	CW_9
		CALL	ADD_TO_LAZY_LIST
		JMP	CHT_REFERENCE


		.CONST

		ALIGN	4

COMDAT_HARD_TABLE	LABEL	DWORD
		;
		;
		;
		DD	CHT_UNDEF		;UNDEFINED, MAKE HARD EXTERN
		DD	CHT_REFERENCE		;ASEG, IGNORE
		DD	CHT_REFERENCE		;RELOC, IGNORE
		DD	CHT_IGNORE		;NEAR COMDEF, IGNORE
		DD	CHT_IGNORE		;FAR COMDEF, IGNORE
		DD	CHT_IGNORE		;HUGE COMDEF, IGNORE
		DD	CHT_REFERENCE		;CONST, IGNORE
		DD	CHT_REF_LIB		;LIBRARY, REFERENCE IT
		DD	CHT_REFERENCE		;IMPORT, IGNORE
		DD	CHT_REFERENCE		;PROMISED, IGNORE
		DD	CHT_IGNORE		;EXTERNAL, IGNORE
		DD	CHT_WEAK		;WEAK, DID I REFERENCE IT WEAKLY?
		DD	CHT_POSWEAK		;POSWEAK, DID I REFERENCE IT POSWEAKLY?
		DD	CHT_IGNORE		;LIB_LIST, IGNORE
		DD	CHT_REF_IMP		;__imp__, REFERENCE IT
		DD	CHT_IGNORE		;ALIASED, IGNORE
		DD	REF_COMDAT		;UH-OH, HERE COMES ANOTHER...
		DD	CHT_WEAK_DEFINED	;WEAK-DEFINED, DID I REFERENCE IT WEAKLY?
		DD	CHT_WEAK_UNREF		;WEAK-UNREF, DID I REFERENCE IT WEAKLY?
		DD	CHT_ALIASED_UNREF	;ALIAS_UNREF, PUT IN ALIASED LIST
		DD	CHT_POSLAZY		;POSLAZY, DID I REFERENCE IT POSLAZILY?
		DD	CHT_LAZY		;LAZY, DID I?
		DD	CHT_LAZY_UNREF		;DID I?
		DD	CHT_IGNORE		;ALIAS-DEFINED, IGNORE
		DD	CHT_LAZY_DEFINED	;LAZY-DEFINED, DID I?
		DD	CHT_NCOMM_UNREF		;REFERENCE IT
		DD	CHT_FCOMM_UNREF		;REFERENCE IT
		DD	CHT_HCOMM_UNREF		;REFERENCE IT
		DD	CHT_IGNORE
		DD	CHT_IGNORE		;UNDECORATED, IGNORE

.ERRNZ ($-COMDAT_HARD_TABLE)-NSYM_SIZE*2


		.CODE	PASS1_TEXT

REF_COMDAT	ENDP


CHECK_WEAKNESS	PROC	NEAR
		;
		;WAS  THIS WEAK WHEN I REFERENCED IT?
		;
		MOV	EDX,[EDI-8]

		CMP	DL,NSYM_POS_WEAK
		JZ	L9$			;IT WAS POS_WEAK, WE ARE OK

		AND	DH,MASK S_WEAK_AGAIN
		JZ	L8$

		CMP	AL,AL

		RET

L8$:
		TEST	ESP,ESP
L9$:
		RET

CHECK_WEAKNESS	ENDP


CHECK_LAZINESS	PROC	NEAR
		;
		;
		;
		MOV	EDX,[EDI-8]		;AL IS NSYM_TYPE, AH IS REF_FLAGS FROM THE GOOD OLD DAYS

		CMP	DL,NSYM_POS_WEAK
		JZ	L9$			;IT WAS POS_WEAK, WE ARE OK

		CMP	DL,NSYM_POS_LAZY
		JZ	L9$

		AND	DH,MASK S_WEAK_AGAIN
		JZ	L8$

		CMP	AL,AL
		RET

L8$:
		TEST	ESP,ESP
L9$:
		RET

CHECK_LAZINESS	ENDP

		public	_reference_impsym
_reference_impsym	proc
		mov	ECX,8[ESP]
		mov	EAX,4[ESP]
_reference_impsym	endp

REFERENCE_IMPSYM	PROC
		;
		;ECX  EAX  IS SYMBOL
		;
		;MAKE A HARD REFERENCE TO THIS SYMBOL WITHOUT THE __imp__
		;
		PUSHM	EDI,ESI

		LEA	ESI,[ECX]._S_NAME_TEXT+6
		PUSH	EAX		;SAVE THIS GINDEX

;		MOV	EAX,OFF REFERENCING_IMPSYM
;		CALL	SAY_OMF_VERBOSE

		MOV	EDI,OFF SYMBOL_TEXT
		CALL	MOVE_ASCIZ_ESI_EDI

;		MOV	EAX,OFF DID_MOVE_ASCIZ
;		CALL	SAY_OMF_VERBOSE

		MOV	ESI,OFF SYMBOL_TEXT
		MOV	EAX,EDI

		SUB	EAX,ESI
		MOV	EDI,ESI

		PUSH	EBX
		MOV	SYMBOL_LENGTH,EAX

;		PUSH	EAX
;		MOV	EAX,OFF DOING_OPTI_MOVE
;		CALL	SAY_OMF_VERBOSE
;		POP	EAX

		CALL	OPTI_MOVE

;		CALL	SAY_OMF_SYMBOL_VERBOSE

		CALL	FAR_INSTALL

		PUSH	EAX
		CALL	PROCESS_EXTRN_ECXEAX

		AND	BL,NOT (MASK S_HARD_REF+MASK S_SOFT_REF+MASK S_WEAK_AGAIN)
		POP	EDX

		MOV	[ECX]._S_REF_FLAGS,BL
		POP	EBX

		POPM	ECX,ESI

		MOV	EAX,ECX
		CONVERT	ECX,ECX,SYMBOL_GARRAY
		ASSUME	ECX:PTR SYMBOL_STRUCT
		POP	EDI

		MOV	[ECX]._S__IMP__DEFAULT_GINDEX,EDX

		TEST	[ECX]._S_REF_FLAGS,MASK S_REFERENCED  ;may have recursed...
                JNZ     L1$

		JMP	ADD_TO__IMP__LIST

L1$:            RET


;		PUSH	EAX
;		MOV	EAX,OFF RETURNED
;		CALL	SAY_OMF_VERBOSE
;		POP	EAX
;		RET

REFERENCE_IMPSYM	ENDP

if	debug

SAY_OMF_SYMBOL_VERBOSE	PROC

		BITT	VERBOSE_OMF
		JZ	L9$

		PUSHM	EDX,EAX

		MOV	ECX,SYMBOL_LENGTH
		MOV	EAX,OFF SYMBOL_TEXT-1

		PUSH	ECX
		MOV	SYMBOL_TEXT-1,CL

		CALL	SAY_VERBOSE

		MOV	EAX,OFF REFERENCED
		CALL	SAY_VERBOSE

		POP	SYMBOL_LENGTH

		POPM	EAX,EDX
L9$:
		RET

SAY_OMF_SYMBOL_VERBOSE	ENDP


		.CONST

PROCESSING_EXTRN	DB	SIZEOF PROCESSING_EXTRN-1,'Processing EXTRN',0DH,0AH
REFERENCED		DB	SIZEOF REFERENCED-1,' Referenced',0DH,0AH
REFERENCING_IMPSYM	DB	SIZEOF REFERENCING_IMPSYM-1,'Referencing IMPSYM',0DH,0AH
RETURNED		DB	SIZEOF RETURNED-1,'Returned',0DH,0AH
SAY_OMF_TYP_MSG		DB	SIZEOF SAY_OMF_TYP_MSG-1+6,'NSYM_TYPE ='
OMF_TYPE_HEX			DB	'XXXX',0DH,0AH
DID_MOVE_ASCIZ		DB	SIZEOF DID_MOVE_ASCIZ-1,'Did MOVE_ASCIZ',0DH,0AH
DOING_OPTI_MOVE		DB	SIZEOF DOING_OPTI_MOVE-1,'Doing OPTI_MOVE',0DH,0AH
endif

		END

